/*
  学习目标：动态路由的基本使用
*/
import React from 'react';
import {
  BrowserRouter as Router,
  Route,
  NavLink,
  Switch,
  Redirect,
} from 'react-router-dom';
import './index.css';
import { Link } from 'react-router-dom';

export default class App extends React.Component {
  render() {
    return (
      <Router>
        <div>
          <h1>app组件</h1>
          <NavLink to="/home" exact>
            调到home
          </NavLink>
          <br />
          <NavLink to="/my">调到我的音乐</NavLink>
          <br />
          <NavLink to="/friend">调到朋友</NavLink>
          <Switch>
            <Redirect from="/" to="/home" exact />
            <Route path="/home" component={Home}></Route>
            {/* 💥 动态路由必须传值， 否则无法匹配 */}
            {/* 1. 将path改造为动态路由 */}
            <Route path="/my/:xxx" component={MyMusic}></Route>
            <Route path="/friend" component={Friend}></Route>
            <Route component={NotFount}></Route>
          </Switch>
          <Header />
        </div>
      </Router>
    );
  }
}

function MyMusic(props) {
  // 2. 通过props.match接收动态路由参数
  return <h1>我是我的音乐件 - {props.match.params.xxx}</h1>;
}

function Home(props) {
  return (
    <>
      <h1>我是首页组件</h1>
      <button
        onClick={() => {
          props.history.go(-2);
          props.history.goBack();
        }}
      >
        点我跳转
      </button>
    </>
  );
}

function Header() {
  return <h1>Header</h1>;
}

function Friend() {
  return (
    <div>
      <Link to="/friend">F1</Link>
      <br />
      <Link to="/friend/f2">F2</Link>
      <br />
      <Link to="/friend/f3">F3</Link>
      <h1>我是朋友组件</h1>
    </div>
  );
}

function NotFount() {
  return <h1>...404..页面</h1>;
}
